<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of accessory_model
 *
 * @author Administrator
 */
class accessory_model extends CI_Model {

    //put your code here
    var $accessory_id = 0;
    var $accessory_name = '';
    var $accessory_detail = '';
    var $accessory_name_segment = '';
    var $accessory_tag = '';
    var $accessory_national = '';
    var $accessory_support = '';
    var $accessory_price = 0;
    var $accessory_guarantee = '';

    function __construct() {
        parent::__construct();
    }

    function getAllAccessories() {
        $query = $this->db->get_where(tbl_accessory,array('accessory_is_delete'=>0));
        return $query->result();
    }

    function getAccessoryByID($id) {
        $query = $this->db->get_where(tbl_accessory, array('accessory_id' => $id,'accessory_is_delete'=>0));
        $count = $query->num_rows();
        $arr = $query->result();
        if ($count <= 0)
            return null;
        return $arr[0];
    }

    function getAccessoryByCategory($category_id) {
        $query = $this->db->get_where(tbl_accessory, array('category_id' => $category_id,'accessory_is_delete'=>0));
        return $query->result();
    }

    function search($product_name, $category_id, $price_from, $price_to) {


        $this->db->like('accessory_name', $product_name);
        $this->db->or_like('accessory_name_segment', $product_name);
        if (is_numeric($category_id)) {
            $this->db->where('category_id', $category_id);
        }
        if (is_numeric($price_from)) {
            $this->db->where('accessory_price >= ', $price_from);
        }
        if (is_numeric($price_to)) {
            $this->db->where('accessory_price <= ', $price_to);
        }
//       $this->db->like('accessory_name',$product_name);
//
//       $this->db->or_like('accessory_name_segment',$product_name);
//        
        $this->db->order_by('accessory_price', 'desc');

        $query = $this->db->get(tbl_accessory);

        return $query->result();
    }

    function getAllAccessories_Admin($page = 0, $pagesize = PAGE_SIZE) {
        $this->db->limit($pagesize, $page);
        $this->db->select('a.*,c.category_name');
        
        $this->db->where('a.accessory_is_delete',0);
        $this->db->from(tbl_accessory . ' a');
        $this->db->join(tbl_accessory_category . ' c', 'c.category_id=a.category_id');
        $query = $this->db->get();


        $this->db->select('accessory_id');
        $this->db->from(tbl_category);
        $total = $this->db->count_all_results();

        $data = array('list' => $query->result(), 'total' => $total);
        return $data;
    }

    function addAccessory($info) {
        return $this->db->insert(tbl_accessory, $info);
    }

    function updateAccessory($info, $id) {
        $this->db->where('accessory_id', $id);
        return $this->db->update(tbl_accessory, $info);
    }
    
    function deleteAccessory($id){
        $this->db->where('accessory_id',$id);
        return $this->db->update(tbl_accessory,array('accessory_is_delete'=>1));
    }

}

?>
